package 动态规划9月;

import java.util.HashSet;
import java.util.Set;
// https://leetcode-cn.com/problems/jewels-and-stones/
// https://leetcode-cn.com/problems/jewels-and-stones/solution/bao-shi-yu-shi-tou-java-by-sui-ji-guo-ch-7wlf/
public class 字符串哈希771宝石与石头0922 {
    // class Solution {
//     public int numJewelsInStones(String jewels, String stones) {
//         int jewelsCount = 0;
//         int jewelsLength = jewels.length(), stonesLength = stones.length();
//         for (int i = 0; i < stonesLength; i++) {
//             char stone = stones.charAt(i);
//             for (int j = 0; j < jewelsLength; j++) {
//                 char jewel = jewels.charAt(j);
//                 if (stone == jewel) {
//                     jewelsCount++;
//                     break;
//                 }
//             }
//         }
//         return jewelsCount;
//     }
// }

    class Solution {
        public int numJewelsInStones(String jewels, String stones) {
            int jewelsCount = 0;
            Set<Character> jewelsSet = new HashSet<Character>();
            int jewelsLength = jewels.length(), stonesLength = stones.length();
            for (int i = 0; i < jewelsLength; i++) {
                char jewel = jewels.charAt(i);
                jewelsSet.add(jewel);
            }
            for (int i = 0; i < stonesLength; i++) {
                char stone = stones.charAt(i);
                if (jewelsSet.contains(stone)) {
                    jewelsCount++;
                }
            }
            return jewelsCount;
        }
    }

}
